#include "MainWindow.h"
#include<stdio.h>
MainWindow::MainWindow(QWidget *parent): QWidget(parent)
{
	m_layout = new QVBoxLayout(this);
	setLayout(m_layout);

	m_upperWidget = new QWidget(this);
	m_layout->addWidget(m_upperWidget);

	m_upperLayout = new QHBoxLayout(m_upperWidget);
	m_upperWidget->setLayout(m_upperLayout);
	
	m_guidLabel = new QLabel("entryorguid:", m_upperWidget);
	m_upperLayout->addWidget(m_guidLabel);

	m_guid = new QLineEdit(m_upperWidget);
	m_upperLayout->addWidget(m_guid);
	m_guid->setValidator(new QIntValidator(this));
	
	m_sourceTypeWidget = new QWidget(this);
	m_layout->addWidget(m_sourceTypeWidget);
	
	m_sourceTypeLayout = new QHBoxLayout(m_sourceTypeWidget);
	m_sourceTypeWidget->setLayout(m_sourceTypeLayout);
	
	m_sourceTypeLabel = new QLabel("SourceType:", m_sourceTypeWidget);
	m_sourceTypeLayout->addWidget(m_sourceTypeLabel);
	
	m_sourceTypeCombo = new QComboBox(m_sourceTypeWidget);
	m_sourceTypeLayout->addWidget(m_sourceTypeCombo);
	m_sourceTypeCombo->addItem("Creature");
	m_sourceTypeCombo->addItem("GameObject");
	m_sourceTypeCombo->addItem("AreaTrigger");
	m_sourceTypeCombo->addItem("Event");
	m_sourceTypeCombo->addItem("Gossip");
	m_sourceTypeCombo->addItem("Quest");
	m_sourceTypeCombo->addItem("Spell");
	m_sourceTypeCombo->addItem("Transport");
	m_sourceTypeCombo->addItem("Instance");
	m_sourceTypeCombo->addItem("TimedActionList");
	
	m_addButton = new QPushButton("Expert mode", this);
	m_layout->addWidget(m_addButton);
	connect(m_addButton, SIGNAL(clicked()), this, SLOT(slotAddClicked()));

	QPushButton *button = new QPushButton("Wizard", this);
	m_layout->addWidget(button);
	connect(button, SIGNAL(clicked()), this, SLOT(slotAddAlternativeClicked()));

	m_widget = new QWidget(this);
	m_layout->addWidget(m_widget);

	m_innerLayout = new QVBoxLayout(m_widget);
	m_widget->setLayout(m_innerLayout);
	
	m_buttonGenerate = new QPushButton("Generate SQL!");
	m_layout->addWidget(m_buttonGenerate);
	connect(m_buttonGenerate, SIGNAL(clicked()), this, SLOT(slotGenerateClicked()));
}

MainWindow::~MainWindow()
{
    
}

void MainWindow::slotAddClicked(){
	if(!m_guid->hasAcceptableInput() || m_guid->text().isEmpty()){
		QMessageBox::warning(this, "Nope!", "Sorry dude, but I'm not letting you do this shit without filling out entryorguid! We need to know which creature this is about!");	
		return;
	}
	ScriptGenerator *generator = new ScriptGenerator(this);
	if( !generator->exec() ){
		delete generator;
		return;
	}
	QString sql, comment;
	generator->getSQL(sql, comment);
	delete generator;
	//sql += m_guid->text() + ", " + QString().setNum(m_sourceTypeCombo->currentIndex()) + ");";
	ScriptEntry *entry = new ScriptEntry(sql, comment, m_widget);
	connect(entry, SIGNAL(deleteMe()), this, SLOT(deleteMeClicked()));
	m_innerLayout->addWidget(entry);
	m_entries.push_back(entry);
}

void MainWindow::slotAddAlternativeClicked(){
	if(!m_guid->hasAcceptableInput() || m_guid->text().isEmpty()){
		QMessageBox::warning(this, "Nope!", "Sorry dude, but I'm not letting you do this shit without filling out entryorguid! We need to know which creature this is about!");	
		return;
	}
	QMessageBox message(this);
	message.setWindowTitle("Expert mode?");
	message.setText("Please select if you want to view this wizard in advanced or beginner's mode");
	message.addButton("Beginner mode", QMessageBox::RejectRole);
	message.addButton("Advanced mode", QMessageBox::AcceptRole);
	bool advanced = message.exec();
    ScriptGeneratorWizard *wizard = new ScriptGeneratorWizard(!advanced, this);
	if(wizard->exec()){
		QString sql, comment;
		ScriptEntryRaw *script = wizard->getSQL(sql, comment);
		sql.prepend("DELETE FROM smart_scripts WHERE entryorguid = " + m_guid->text() + ";\n");
		ScriptEntry *entry = new ScriptEntry(sql, comment, script, m_widget);
		connect(entry, SIGNAL(deleteMe()), this, SLOT(deleteMeClicked()));
		connect(entry, SIGNAL(editMe()), this, SLOT(editMeClicked()));
		m_innerLayout->addWidget(entry);
		m_entries.push_back(entry);
	}
	delete wizard;
}

void MainWindow::slotGenerateClicked(){
	QString sql("DELETE FROM smart_scripts WHERE entryorguid = " + m_guid->text() + ";\n");
	for(int i = 0; i < m_entries.size(); i++)
		sql += m_entries[i]->sql() + m_guid->text() + ", " + QString().setNum(m_sourceTypeCombo->currentIndex()) + ", " + QString().setNum(i) + ");\n";
    switch(m_sourceTypeCombo->currentIndex()){
        case SMART_SCRIPT_TYPE_CREATURE:
            sql += "UPDATE creature_template SET AIName = \"SmartAI\" WHERE entry = " + m_guid->text() + ";\n";
            break;
        case SMART_SCRIPT_TYPE_GAMEOBJECT:
            sql += "UPDATE gameobject_template SET AIName = \"SmartGameObjectAI\" WHERE entry = " + m_guid->text() + ";\n";
            break;
        case SMART_SCRIPT_TYPE_AREATRIGGER:
        sql += "REPLACE INTO areatrigger_scripts (entry, ScriptName) VALUES (" + m_guid->text() + ", SmartTrigger);\n";
            break;
        case SMART_SCRIPT_TYPE_EVENT:
            break;
        case SMART_SCRIPT_TYPE_GOSSIP:
            break;
        case SMART_SCRIPT_TYPE_QUEST:
            break;
        case SMART_SCRIPT_TYPE_SPELL:
            break;
        case SMART_SCRIPT_TYPE_TRANSPORT:
            break;
        case SMART_SCRIPT_TYPE_INSTANCE:
            break;
        case SMART_SCRIPT_TYPE_TIMED_ACTIONLIST:
            break;
        default:
            QMessageBox::critical(0, "Bad shit happened!", "Something terrible happened while selecting sourceType. Please contact Sanity!");
    }

	QTextEdit *edit = new QTextEdit(0);
	edit->setWindowTitle("SQL output");
	edit->setText(sql);
	edit->setReadOnly(true);
	edit->selectAll();
	edit->copy();
	edit->show();
}

void MainWindow::deleteMeClicked(){
	QObject *s = sender();
	for(int i = 0; i < m_entries.size(); i++){
		if(m_entries[i] == s){
			m_innerLayout->removeWidget((QWidget*)s);
			delete m_entries.takeAt(i);
			return;
		}
	}
}

void MainWindow::editMeClicked(){
	QObject *s = sender();
	for(int i = 0; i < m_entries.size(); i++){
		if(m_entries[i] == s){
			ScriptGenerator *generator = new ScriptGenerator(m_entries[i]->script(), this);
			if( !generator->exec() ){
				delete generator;
				return;
			}
			QString sql, comment;
			generator->getSQL(sql, comment);
			delete generator;
			//sql += m_guid->text() + ", " + QString().setNum(m_sourceTypeCombo->currentIndex()) + ");";
			ScriptEntry *entry = new ScriptEntry(sql, comment, m_widget);
			connect(entry, SIGNAL(deleteMe()), this, SLOT(deleteMeClicked()));
			
			m_innerLayout->removeWidget((QWidget*)s);
			delete m_entries.takeAt(i);
			
			m_innerLayout->insertWidget(i, entry);
			m_entries.push_back(entry);
			return;
		}
	}
}
